﻿
Namespace Controle

    <ToolboxData("<{0}:YCFileUploadAll runat=""server""></{0}:YCFileUploadAll>")> _
    Public Class YCFileUploadAll
        Inherits FileUpload

#Region "Variaveis"

        Private pTipoUpload As EnumTipoUpload = EnumTipoUpload.Todos
        Private pLocalSalvar As String = Nothing
        Private pExtensaoArquivo As String = Nothing
        Private pExtensaoArquivoBloqueado As String = Nothing
        Public Event UploadIniciado()
        Public Event UploadCompletado()
        Private pNomeArquivo As String
        Private pCancelarUpload As Boolean = False
        Private pAutoPostBack As Boolean = True

#End Region

#Region "Enum"

        Public Enum EnumTipoUpload
            Todos
            Image
            TXT
            Zip
        End Enum

#End Region

#Region "Propriedades"

        Public Property TipoUpload() As EnumTipoUpload
            Get
                Return pTipoUpload
            End Get
            Set(ByVal value As EnumTipoUpload)
                pTipoUpload = value
            End Set
        End Property

        Public Property LocalSalvar() As String
            Get
                Return pLocalSalvar
            End Get
            Set(ByVal value As String)
                pLocalSalvar = value.Replace("/", "\")
            End Set
        End Property

        ''' <summary>
        ''' Coloque as extensões dos arquivos permitidos separados por ","(sem espaço)
        ''' Ex: txt,jpg,png,gif
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property ExtensaoArquivo() As String
            Get
                Return pExtensaoArquivo
            End Get
            Set(ByVal value As String)
                pExtensaoArquivo = value
            End Set
        End Property

        ''' <summary>
        ''' Coloque as extensões dos arquivos permitidos separados por ","(sem espaço)
        ''' Ex: txt,jpg,png,gif
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Property ExtensaoArquivoBloqueado() As String
            Get
                Return pExtensaoArquivoBloqueado
            End Get
            Set(ByVal value As String)
                pExtensaoArquivoBloqueado = value
            End Set
        End Property

        Public Property NomeArquivo() As String
            Get
                Return pNomeArquivo
            End Get
            Set(ByVal value As String)
                pNomeArquivo = value
            End Set
        End Property

        Public Property CancelarUpload() As Boolean
            Get
                Return pCancelarUpload
            End Get
            Set(ByVal value As Boolean)
                pCancelarUpload = value
            End Set
        End Property

        Public Property AutoPostBack() As Boolean
            Get
                Return pAutoPostBack
            End Get
            Set(ByVal value As Boolean)
                pAutoPostBack = value
            End Set
        End Property

#End Region

        Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
            MyBase.OnPreRender(e)
            If pAutoPostBack Then _
                Me.Attributes.Add("onchange", String.Concat("if(confirm(""Deseja enviar esse arquivo ?""))__doPostBack('','", Me.ClientID, "'); else this.value = """";"))
        End Sub

        Private Sub YCFileUploadAll_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If (Page.Request.Form("__EVENTARGUMENT") IsNot Nothing AndAlso Page.Request.Form("__EVENTARGUMENT") = Me.ClientID) Then
                If Me.HasFile Then
                    Dim Nome() As String = Regex.Split(Me.PostedFile.FileName, "([\w][^\\]+)\.([\w]+)$")

                    If (pExtensaoArquivo IsNot Nothing AndAlso Not pExtensaoArquivo.Split(",").Contains(Nome(2))) OrElse (pExtensaoArquivoBloqueado IsNot Nothing AndAlso pExtensaoArquivoBloqueado.Split(",").Contains(Nome(2))) Then
                        YCFramework.Web.Javascript.Alert("Extensão do arquivo não suportado.")
                    Else
                        pNomeArquivo = Nome(1) & "." & Nome(2)

                        RaiseEvent UploadIniciado()

                        If Not CancelarUpload Then
                            Me.PostedFile.SaveAs(pLocalSalvar & pNomeArquivo)

                            RaiseEvent UploadCompletado()
                        End If

                    End If
                Else
                    YCFramework.Web.Javascript.Alert("Não tem arquivo.")
                End If
            End If
        End Sub


        Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
            With writer
                .Write("<span style=""display:block;overflow:hidden;position:relative;width:140px;border-left:2px groove #D4D0C8;"">")
                ' .Write(String.Format("<img src=""{0}"" style=""width: 45px; position:absolute;"" />", Page.ClientScript.GetWebResourceUrl(GetType(YCTextBox), "YCFramework.iconeYCUpload.png")))

                MyBase.Style.Add("position", "relative")
                MyBase.Style.Add("right", "80px")
                MyBase.Render(writer)

                .Write("</span>")
            End With

        End Sub


    End Class

End Namespace